<?PHP

	include("../includes/configPages.php");
	include("../includes/forum/category.class.php");
	include("../includes/forum/board.class.php");
	include("../includes/forum/topic.class.php");
	include("../includes/forum/post.class.php");
	$category = new category();
	$board = new board();
	$topic = new topic();
	$post = new post();

	$outHtml = "";
	
	if(is_numeric($_GET['args'])){
		$topicId = $db->prep($_GET['args']);
		
		$allPosts = $post->getAllPosts($db, $topicId);
		$variableStack['topic_posts'] = "";
		if(mysql_num_rows($allPosts)==0){
			$html = file_get_contents('../styles/'.$activeStyle.'/t/board-none.html');
			$variableStack['board_name'] = "Thread does not exist";
			$variableStack['board_desc'] = "Sorry!";
			echo parseHtml($html, $variableStack);
		} else {
			$count = 0;
			
			$breadRes = $post->getBread($db, $topicId);
	
			/* pagination jquery stuff */
			if($breadRes!=false){
				$variableStack['topic_link'] = "#topic:".$breadRes['top_id']; ; 
				$variableStack['topic_text'] = $breadRes['top_name']; 
				$variableStack['topic_id'] = $breadRes['top_id']; 
				$variableStack['board_link'] = "#board:".$breadRes['boa_id']; 
				$variableStack['board_text'] = $breadRes['boa_name']; 
				$variableStack['board_id'] = $breadRes['boa_id']; 
				$variableStack['category_link'] = "#forum"; 
				$variableStack['category_text'] = $breadRes['cat_name']; 
				$variableStack['category_id'] = $breadRes['cat_id']; 
			
				/* topic info */
				$topicInfo = $topic->getTopic($db, $topicId);
				while($topicRow = mysql_fetch_object($topicInfo)){
					if($topicRow->locked==0 || ($topicRow->locked==1 && $users->auth($db,2))){
						$variableStack['locked_tag'] = "";
						$variableStack['icon_tag'] = "";
						$variableStack['reply_text'] = "reply to topic";
					} else {
						$variableStack['locked_tag'] = "_locked";
						$variableStack['icon_tag'] = "_locked";
						$variableStack['reply_text'] = "topic locked";
					}
					$stickied = $topicRow->topic_stickied;
				}
					
						while($row = mysql_fetch_object($allPosts)){
							if($row->post_deleted==0 || ($row->post_deleted==1 && $users->auth($db,2)) || ($row->post_deleted==1 && $row->user_id==$_SESSION['uid'])){
								$html = file_get_contents('../styles/'.$activeStyle.'/t/topic_post.html');
								$postVariableStack['post_title'] = stripslashes($row->post_name);
								$postVariableStack['post_id'] = $row->post_id;
								$postVariableStack['post_message'] = stripslashes($row->post_content_html);
								$postVariableStack['post_number'] = $count;
								$postVariableStack['post_time'] = date('r', $row->post_time);
								$postVariableStack['id'] = $row->user_id;
								$uid = $row->user_id;
								/* mod controls */
								if($users->auth($db, 2) || $row->user_id==$_SESSION['uid']){
									if($count==0){
										// mod only part
										if($users->auth($db, 2)){
											if($stickied==0){
												$postVariableStack['mod_controls']	= '<div dd="unstuck" tt="'.$row->topic_id.'" title="Sticky this thread" id="sticky_'.$row->topic_id.'" class="sticky_thread ui-state-default ui-corner-all">sticky thread</div>';
											} else {
												$postVariableStack['mod_controls']	= '<div dd="stuck" tt="'.$row->topic_id.'" title="Unsticky this thread" id="sticky_'.$row->topic_id.'" class="sticky_thread ui-state-default ui-corner-all">unsticky thread</div>';
											}
										} else {
											$variableStack['stick_tag']	= '';
										}
										
									} else {
										$variableStack['stick_tag'] = '';
									}
									if($config->getVal($db, 'allow_edit')==1 || $users->auth($db, 2)){
										$postVariableStack['mod_controls'] .= '<div tt="'.$row->post_id.'" title="Edit this reply" id="edit_reply_'.$row->post_id.'" class="edit_reply ui-state-default ui-corner-all">edit</div>';	
									}
									if($config->getVal($db, 'allow_delete')==1 || $users->auth($db, 2)){
										if($row->post_deleted==0){
											$postVariableStack['mod_controls'] .= '<div dd="undeleted" tt="'.$row->post_id.'" title="Delete this reply" id="delete_reply_'.$row->post_id.'" class="delete_reply ui-state-default ui-corner-all"><span class="ui-icon ui-icon-close"></span></div>';	
										} else {
											$postVariableStack['mod_controls'] .= '<div dd="deleted" tt="'.$row->post_id.'" title="Undelete this reply" id="delete_reply_'.$row->post_id.'" class="delete_reply ui-state-default ui-corner-all"><span class="ui-icon ui-icon-close"></span></div>';	
										}
									}
								}
								
								$userResult = $post->getUserPostInfo($db, $row->user_id);
								if(mysql_num_rows($userResult)==0){
									//anon
								} else{
									//user
									while($row2=mysql_fetch_object($userResult)){
										$postVariableStack['username'] = $row2->username;				
										$postVariableStack['rname'] = $row2->rname;												
										$postVariableStack['rank'] = $row2->rankname;

										if($row2->rankcolour==NULL){
											$postVariableStack['user_colour'] = $row2->colour;
										} else {
											$postVariableStack['user_colour']=$row2->rankcolour;
										}
										
										if( $row2->user_avatar==""){
											$postVariableStack['user_avatar'] ="images/no-avatar.gif";
										} else {
											$postVariableStack['user_avatar'] = $row2->user_avatar;	
										}
									}
								}
								
								if($count % 10 == 0 && $count!=0){
									$variableStack['topic_posts'] .= '</div><div class="result">';
								}

								if($row->post_deleted==1 && ($users->auth($db,2) || $row->user_id==$_SESSION['uid'])){
									$postVariableStack['deleted_tag'] = "_deleted";
									$userResult = $post->getUserPostInfo($db, $row->post_deleted_by_id);
									while($row2=mysql_fetch_object($userResult)){
										$postVariableStack['deleted_by'] = "<br>deleted by ".$row2->username;
									}
								} else if($row->post_deleted==1){
									$postVariableStack['deleted_tag'] = "_hide";															 
									$postVariableStack['deleted_by'] = "_hide";
								} else {
									$postVariableStack['deleted_tag'] = "";															 
									$postVariableStack['deleted_by'] = "";
								}
								/* sigs */
								$sig = $users->getSignature($db, $row->user_id);
								if($sig){
									$postVariableStack['signature'] = mysql_result($sig, 0, 'sig_html');
								} else {
									$postVariableStack['signature'] = "";
								}
								$variableStack['topic_posts'] .= parseHtml($html, $postVariableStack);
								$postVariableStack['mod_controls']="";
								$count++;
							}
						}
									
				$html = file_get_contents('../styles/'.$activeStyle.'/t/topic.html');
				$outHtml .= parseHtml($html, $variableStack);	
				
				echo $outHtml;
			} else {
				$html = file_get_contents('../styles/'.$activeStyle.'/t/board-none.html');
				$variableStack['board_name'] = "Topic does not exist";
				$variableStack['board_desc'] = "Sorry!";
				echo parseHtml($html, $variableStack);
			}
	
		}
	} else {
		$html = file_get_contents('../styles/'.$activeStyle.'/t/board-none.html');
		$variableStack['board_name'] = "Topic does not exist";
		$variableStack['board_desc'] = "Sorry!";
		echo parseHtml($html, $variableStack);
	}
	
	
?>